/*
 * Created on 02.07.2006
 */
package com.xlabs.client.richtext;


import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.*;
import com.google.gwt.user.client.ui.*;

/**
 * This class represent a tooltip that can be setup with css. The used css stylename is Tooltip.
 * A typical use is:
 * <code><pre>
 *      yourWidget.addMouseListener(new Tooltip(tooltip));
 * </pre></code>
 * 
 * @author Volker Berlin
 */
public class Tooltip implements MouseOverHandler, MouseOutHandler, MouseMoveHandler, MouseDownHandler {

    final SimplePanel tooltip;
    private Timer timer;
    private int lastX;
    private int lastY;
    boolean isShowing;
    
    /**
     * The Constructor of the Tooltip.
     * @param text The tooltip text. It should not be null.
     */
    public Tooltip(String text) {
        tooltip = new SimplePanel(){};
        tooltip.add( new HTML(text) );
        tooltip.setStyleName("Tooltip");
    }
    
    public void onMouseOver(MouseOverEvent event) {
        if(timer == null){
            timer = new Timer(){
                public void run() {
                    show();
                }
            };
        }
        timer.schedule(1000);
    }
    
    public void onMouseMove(MouseMoveEvent event) {
    	Widget sender = (Widget) event.getSource();
        int left = sender.getAbsoluteLeft() + event.getX() + getScrollLeft() - 2;
        int top = sender.getAbsoluteTop() + event.getY() + getScrollTop() +20;
        
        if(left != lastX || top != lastY){
            if(!isShowing){
                return;
            }
            lastX = left;
            lastY = top;
        }
    }

    public void onMouseOut(MouseOutEvent event) {
        if(timer != null){
            timer.cancel();
        }
        if (tooltip != null) {
            hide();
            isShowing = false;
        }
    }
    
    public void onMouseDown(MouseDownEvent event) {
        onMouseOut(null);
    }

    /**
     * Show the tooltip.
     */
    void show(){
        if (isShowing){
            return;
        }
        isShowing = true;

        RootPanel.get().add(tooltip);
    }
    
    /**
     * Hide the tooltip.
     */
    private void hide() {
        if (!isShowing){
            return;
        }
        isShowing = false;

        RootPanel.get().remove(tooltip);
    }
    
    
    /**
     * A hack for a bug in GWT with the scroll location
     */
    static native int getScrollTop()/*-{
        return $doc.body.scrollTop;
    }-*/;
    
    /**
     * A hack for a bug in GWT with the scroll location
     */
    static native int getScrollLeft()/*-{
        return $doc.body.scrollLeft;
    }-*/;
} 
